using Microsoft.AspNetCore.Mvc;
using FarmDemo.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;

namespace FarmDemo.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class HistoryController : ControllerBase
{
    private readonly DataSet db;
    HistoryController(DataSet db)
    {
        this.db = db;
    }

    [HttpGet("{uid}")]
    public async Task<IActionResult> GetHistoryByUid(int uid)
    {
        // 创建一个 LINQ 查询，根据 uid 查询 HistoryList 表和 History 表的联合结果
        var query = from hl in db.HistoryList
                    join h in db.History on hl.hid equals h.HistoryId
                    where hl.uid == uid
                    select new { h.imageUrl, h.answer };

        // 执行查询，并获取结果列表
        var data = await query.ToListAsync();
        // 判断是否有数据
        if (data.Any())
        {
            // 返回 200 状态码和数据列表
            return Ok(data);
        }
        else
        {
            // 返回 404 状态码和消息
            return NotFound("No history found for this uid.");
        }
    }

}